/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.woxthebox.draglistview.sample.slice;

import com.woxthebox.draglistview.sample.ResourceTable;
import com.woxthebox.draglistview.sample.fraction.HorizontalFractionAbility;
import com.woxthebox.draglistview.sample.fraction.ListFractionAbility;
import com.woxthebox.draglistview.sample.spinner.MaterialSpinner;
import com.woxthebox.draglistview.utils.FinalStaticBean;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;

/**
 * MainAbilitySlice
 *
 * @since 2021-03-27
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String[] SPIN_MUNE = {
        FinalStaticBean.TEST_LIST,
        FinalStaticBean.DISABLEDRAG_STRING,
        FinalStaticBean.GRIDLAYOUT,
        FinalStaticBean.ADDCOLUMN_STRING,
        FinalStaticBean.REMOVECOLUMN_STRING,
        FinalStaticBean.CLEARBOARD_STRING
    };
    private Text text;
    private MaterialSpinner materialSpinner;
    private boolean isBoard = false;
    private boolean isClickedMenu2 = false;
    private ListFractionAbility listFractionAbility;
    private HorizontalFractionAbility horizontalFractionAbility;
    private boolean isEnable = true;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        if (listFractionAbility == null) {
            listFractionAbility = new ListFractionAbility(this);
        }
        if (horizontalFractionAbility == null) {
            horizontalFractionAbility = new HorizontalFractionAbility(this);
        }
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_show_stacklayout, horizontalFractionAbility)
                .submit();

        text = (Text) findComponentById(ResourceTable.Id_show_select_text);

        materialSpinner = (MaterialSpinner) findComponentById(ResourceTable.Id_show_spinner_menu);

        materialSpinner.setItems(SPIN_MUNE);

        materialSpinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                selectLayout(position);
                materialSpinner.setShowFalse();
            }
        });

        materialSpinner.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {
            @Override
            public void onNothingSelected(MaterialSpinner spinner) {
                materialSpinner.setShowFalse();
            }
        });
    }

    private void selectLayout(int position) {
        switch (position) {
            case FinalStaticBean.NUM_0:
                switchZero();
                break;
            case FinalStaticBean.NUM_1:
                switchOne(position);
                break;
            case FinalStaticBean.NUM_2:
                if (SPIN_MUNE[FinalStaticBean.NUM_2].equals("List(drag on touch image icon)")) {
                    listFractionAbility.setLayoutType(FinalStaticBean.LINEARLAYOUTTYPE);
                } else if (SPIN_MUNE[FinalStaticBean.NUM_2].equals(FinalStaticBean.GRIDLAYOUT)) {
                    horizontalFractionAbility.setLayoutType(FinalStaticBean.GRIDLAYOUTHORIZONTALTYPE);
                    SPIN_MUNE[FinalStaticBean.NUM_2] = "List layout";
                } else if (SPIN_MUNE[FinalStaticBean.NUM_2].equals("List layout")) {
                    horizontalFractionAbility.setLayoutType(FinalStaticBean.LINEARLAYOUTTYPE);
                    SPIN_MUNE[FinalStaticBean.NUM_2] = "Grid layout";
                }
                break;
            case FinalStaticBean.NUM_3:
                if (SPIN_MUNE[FinalStaticBean.NUM_3].equals("Grid vertical(drag on long press)")) {
                    listFractionAbility.setLayoutType(FinalStaticBean.GRIDLAYOUTVERTICALTYPE);
                } else if (SPIN_MUNE[FinalStaticBean.NUM_3].equals("Add column")) {
                    horizontalFractionAbility.setLayoutType(FinalStaticBean.ADDCOLUMN);
                }
                break;
            case FinalStaticBean.NUM_4:
                if (SPIN_MUNE[FinalStaticBean.NUM_4].equals("Grid horizontal(drag on long press)")) {
                    listFractionAbility.setLayoutType(FinalStaticBean.GRIDLAYOUTHORIZONTALTYPE);
                } else if (SPIN_MUNE[FinalStaticBean.NUM_4].equals("Remove column")) {
                    horizontalFractionAbility.setLayoutType(FinalStaticBean.REMOVECOLUMN);
                }
                break;
            case FinalStaticBean.NUM_5:
                if (SPIN_MUNE[FinalStaticBean.NUM_5].equals(FinalStaticBean.CLEARBOARD_STRING)) {
                    horizontalFractionAbility.setLayoutType(FinalStaticBean.CLEARBOARD);
                }
                break;
            default:
                break;
        }
    }

    private void switchOne(int position) {
        if (isClickedMenu2) {
            if (horizontalFractionAbility != null) {
                horizontalFractionAbility.setLayoutType(FinalStaticBean.ENABLEDRAG);
            }
            if (listFractionAbility != null) {
                listFractionAbility.setLayoutType(FinalStaticBean.ENABLEDRAG);
            }
            SPIN_MUNE[FinalStaticBean.NUM_1] = FinalStaticBean.DISABLEDRAG_STRING;
        } else {
            if (horizontalFractionAbility != null) {
                horizontalFractionAbility.setLayoutType(FinalStaticBean.DISABLEDRAG);
            }
            if (listFractionAbility != null) {
                listFractionAbility.setLayoutType(FinalStaticBean.DISABLEDRAG);
            }
            SPIN_MUNE[FinalStaticBean.NUM_1] = "Enable drag";
        }
        isClickedMenu2 = !isClickedMenu2;
        materialSpinner.notifynotifyItemChanged(position);
    }

    private void switchZero() {
        if (isBoard) {
            text.setText("Board");
            ((FractionAbility)getAbility()).getFractionManager()
                    .startFractionScheduler()
                    .replace(ResourceTable.Id_show_stacklayout, horizontalFractionAbility)
                    .submit();
            setBoardTrueData();
        } else {
            text.setText("List and Grid");
            ((FractionAbility)getAbility()).getFractionManager()
                    .startFractionScheduler()
                    .replace(ResourceTable.Id_show_stacklayout, listFractionAbility)
                    .submit();
            setBoardFalseData();
        }
        materialSpinner.notifyDataChanged();
        isBoard = !isBoard;
    }

    private void setBoardFalseData() {
        SPIN_MUNE[FinalStaticBean.NUM_0] = "Test board";
        SPIN_MUNE[FinalStaticBean.NUM_1] = "Disable drag";
        SPIN_MUNE[FinalStaticBean.NUM_2] = "List(drag on touch image icon)";
        SPIN_MUNE[FinalStaticBean.NUM_3] = "Grid vertical(drag on long press)";
        SPIN_MUNE[FinalStaticBean.NUM_4] = "Grid horizontal(drag on long press)";
        SPIN_MUNE[FinalStaticBean.NUM_5] = "";
        materialSpinner.setWidth(FinalStaticBean.NUM_840);
    }

    private void setBoardTrueData() {
        SPIN_MUNE[FinalStaticBean.NUM_0] = FinalStaticBean.TEST_LIST;
        SPIN_MUNE[FinalStaticBean.NUM_1] = "Disable drag";
        SPIN_MUNE[FinalStaticBean.NUM_2] = "Grid layout";
        SPIN_MUNE[FinalStaticBean.NUM_3] = "Add column";
        SPIN_MUNE[FinalStaticBean.NUM_4] = "Remove column";
        SPIN_MUNE[FinalStaticBean.NUM_5] = "Clear board";
        materialSpinner.setWidth(FinalStaticBean.NUM_500);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public boolean getEnable() {
        return isEnable;
    }
}
